package com.lange.autocode.projectManager;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.UserThreadLocalUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.IdWorker;
import cn.net.autocode.maths.NumberFormat;
import cn.net.autocode.platform.workflow.WorkflowFactory;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("projectManager/pmDraft")
public class PmDraft {

    private final Dao dao;

    public PmDraft(Dao dao) {
        this.dao = dao;
    }

    @RequestMapping("add")
    public ResultT add(@RequestBody JSONObject param) {
        //项目编码
        String projectCode = dao.queryValue("select project_code from pm_projects where org_code=?",String.class, UserThreadLocalUtils.getOrgCode());
        if(projectCode==null){
            projectCode = "PM0001";
        }else{
            projectCode = "PM"+NumberFormat.IntFormat(Integer.parseInt(projectCode.substring(2))+1,"0000");
        }
        //项目状态
        param.put("status", "Draft");
        param.put("projectCode", projectCode);
        param.put("id", IdWorker.ulid());
        param.put("orgCode", UserThreadLocalUtils.getOrgCode());
        dao.createJSON(param,"pm_projects");

        //创建工作流审批记录
        WorkflowFactory.create("pm",param.getString("id"));
        return ResultT.success(param);
    }
}
